package com.management.demo.controller.wx;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.management.demo.domain.wx.Category;
import com.management.demo.dto.wx.CategoryDTO;
import com.management.demo.dto.wx.CategoryQueryDTO;
import com.management.demo.service.CategoryService;
import com.management.demo.vo.wx.CategoryVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "分类接口")
@RestController
@RequestMapping("wx/category")
public class WXCategoryController {

    @Autowired
    private CategoryService categoryService;

    @Operation(summary = "查询所有分类")
    @PostMapping("list")
    public SaResult list(@RequestBody CategoryQueryDTO categoryQueryDTO) {
        String name = categoryQueryDTO.getName();
        Integer status = categoryQueryDTO.getStatus();
        List<Category> categories = categoryService.list(new LambdaQueryWrapper<Category>()
                .like(StringUtils.isNotBlank(name), Category::getName, name)
                .eq(status != null, Category::getStatus, status)
                .orderByAsc(Category::getSort));
        List<CategoryVO> result = BeanUtil.copyToList(categories, CategoryVO.class);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "新增/修改分类信息")
    @PostMapping("createOrEdit")
    public SaResult createOrEdit(@RequestBody CategoryDTO categoryDTO) {
        Category category = BeanUtil.copyProperties(categoryDTO, Category.class);
        boolean result = categoryService.saveOrUpdate(category);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "根据id获取分类信息")
    @GetMapping("getById/{id}")
    public SaResult getById(@Parameter(description = "分类id")
                            @PathVariable Integer id) {
        Category category = categoryService.getById(id);
        CategoryVO result = BeanUtil.copyProperties(category, CategoryVO.class);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "删除分类信息")
    @DeleteMapping("remove/{id}")
    public SaResult remove(@Parameter(description = "分类id")
                           @PathVariable Integer id) {
        boolean result = categoryService.removeById(id);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "修改分类展示状态")
    @PostMapping("changeStatus/{id}/{status}")
    public SaResult changeStatus(@Parameter(description = "分类id") @PathVariable Integer id,
                                 @Parameter(description = "是否展示") @PathVariable Integer status) {
        UpdateWrapper<Category> categoryUpdateWrapper = new UpdateWrapper<>();
        categoryUpdateWrapper.set("status", status);
        categoryUpdateWrapper.eq("id", id);
        boolean result = categoryService.update(categoryUpdateWrapper);
        return SaResult.ok().setData(result);
    }
}
